package com.atguigu.spzx.manager.service.impl;

import com.atguigu.spzx.manager.mapper.ProductDetailsMapper;
import com.atguigu.spzx.manager.mapper.ProductMapper;
import com.atguigu.spzx.manager.mapper.ProductSkuMapper;
import com.atguigu.spzx.manager.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.entity.product.ProductDetails;
import com.atguigu.spzx.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: ProductServiceImpl
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/20 14:30
 * Version 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductSkuMapper productSkuMapper;

    @Resource
    private ProductDetailsMapper productDetailsMapper;
    //列表（分页查询）
    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page,limit);
        List<Product> list = productMapper.findByPage(productDto);
        return new PageInfo<>(list);
    }

    //添加到数据库喽
    @Override
    public void save(Product product) {
        //1 保存商品的基本信息到product表
        product.setStatus(0);
        product.setAuditStatus(0);
        productMapper.save(product);

        //2 获取商品的sku列表集合，保存sku信息，product_sku表
        List<ProductSku> productSkuList = product.getProductSkuList();
        for(int i = 0; i < productSkuList.size(); i++) {
            ProductSku sku = productSkuList.get(i);
            //商品编号
            sku.setSkuCode(product.getId() + "_" + i);
            //商品id
            sku.setProductId(product.getId());
            //skuName
            sku.setSkuName(product.getName() + sku.getSkuSpec());
            sku.setSaleNum(0);                               // 设置销量
            sku.setStatus(0);
            productSkuMapper.save(sku);
        }
        //3 保存商品的详情数据到product_details表
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);
    }

    //根据商品id查询商品信息
    @Override
    public Product getById(Integer id) {
        //1 根据id查询商品的基本信息 product
        Product product = productMapper.findProductById(id);

        //2 根据商品id查询商品sku信息列表 product_sku
        List<ProductSku> productSkuList = productSkuMapper.findProductSkuByProductId(id);
        product.setProductSkuList(productSkuList);

        //3 根据商品id查询商品详情数据 product_details
        ProductDetails productDetails = productDetailsMapper.findProductDetailsByProductId(id);
        product.setDetailsImageUrls(productDetails.getImageUrls());

        return product;
    }

    //将更改添加到数据库
    @Override
    public void updateById(Product product) {
        //修改product
        productMapper.updateById(product);

        //修改product_sku
        List<ProductSku> productSkuList = product.getProductSkuList();
        productSkuList.forEach(productSku -> {
            productSkuMapper.updateById(productSku);
        });

        //修改product_details
        String detailsImageUrls = product.getDetailsImageUrls();
        ProductDetails productDetails = productDetailsMapper.findProductDetailsByProductId(Math.toIntExact(product.getId()));
        productDetails.setImageUrls(detailsImageUrls);
        productDetailsMapper.updateById(productDetails);

    }

    //删除商品
    @Override
    public void deleteById(Long id) {
        // 根据id删除商品基本数据
        productMapper.deleteById(id);
        // 根据商品id删除商品的sku数据
        productSkuMapper.deleteByProductId(id);
        // 根据商品的id删除商品的详情数据
        productDetailsMapper.deleteByProductId(id);
    }

    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        Product product = new Product();
        product.setId(id);
        if(auditStatus == 1) {
            product.setAuditStatus(1);
            product.setAuditMessage("审批通过");
        } else {
            product.setAuditStatus(-1);
            product.setAuditMessage("审批不通过");
        }
        productMapper.updateById(product);
    }

    //商品上下架
    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if(status == 1) {
            product.setStatus(1);
        } else {
            product.setStatus(-1);
        }
        productMapper.updateById(product);
    }

}
